package mylib

// 基础包
import (
	"fmt"
	"strconv"

	"github.com/henrylee2cn/pholcus/app/downloader/request" //必需
	"github.com/henrylee2cn/pholcus/common/goquery"         //DOM解析
	// "github.com/henrylee2cn/pholcus/logs"               //信息输出
	. "github.com/henrylee2cn/pholcus/app/spider" //必需
	// . "github.com/henrylee2cn/pholcus/app/spider/common"          //选用
	// net包
	// "net/http" //设置http.Header
	// "net/url"
	// 编码包
	// "encoding/xml"
	// "encoding/json"
	// 字符串处理包
	// "regexp"
	//"strconv"
	//"strings"
	// 其他包
	// "fmt"
	// "math"
	// "time"
	//"log"
)

func init() {
	LoroPiana.Register()
}

type news struct {
	module string //模块
	title  string //标题
	href   string //连接
	top    string
}

var LoroPiana = &Spider{
	Name:        "Q房网",
	Description: "房产照片下载",
	// Pausetime: 300,
	// Keyin:   KEYIN,
	// Limit:        LIMIT,
	EnableCookie: false,

	RuleTree: &RuleTree{
		Root: func(ctx *Context) {
			city := []string{}
			city = append(city, "http://shenzhen.qfang.com")
			city = append(city, "http://beijing.qfang.com")
			city = append(city, "http://dongguan.qfang.com")
			city = append(city, "http://foshan.qfang.com")
			city = append(city, "http://guangzhou.qfang.com")
			city = append(city, "http://huizhou.qfang.com")
			city = append(city, "http://qingdao.qfang.com")
			city = append(city, "http://shanghai.qfang.com")
			city = append(city, "http://suzhou.qfang.com")
			city = append(city, "http://zhuhai.qfang.com")
			city = append(city, "http://zhongshan.qfang.com")
			for _, url := range city {
				ctx.AddQueue(&request.Request{
					Method: "GET",
					Url:    url + "/rent",
					Rule:   "分类列表",
					Temp: map[string]interface{}{
						"wangzhi": url,
					},
				})
			}
		},

		Trunk: map[string]*Rule{

			"分类列表": {
				ParseFunc: func(ctx *Context) {
					//fmt.Println("分类列表")

					dom := ctx.GetDom()

					//module := dom.Find(".house-detail .clearfix")
					//fmt.Println(module)

					//最大页码
					var turnpage_num_max string
					turnpage_num := dom.Find(".turnpage_num a")

					turnpage_num.Each(func(i int, s *goquery.Selection) {

						if turnpage_num_max < s.Text() {
							turnpage_num_max = s.Text()
						}
					})
					//fmt.Println(turnpage_num_max)

					ns2, ok := strconv.Atoi(turnpage_num_max)
					//fmt.Println(ns2, nil)
					if ok != nil {
						fmt.Println("转换出错")
						return
					}
					for i := 1; i <= ns2; i++ {
						//for i := 1; i <= 2; i++ {
						//fmt.Println(i)
						var wangzhi string
						wangzhi = ctx.GetTemp("wangzhi", "").(string)
						url := wangzhi + "/rent/f" + strconv.Itoa(i)
						ctx.AddQueue(&request.Request{
							Url:  url,
							Rule: "房产列表",
							Temp: map[string]interface{}{
								"wangzhi": wangzhi,
							},
						})

					}
				},
			},
			"房产列表": {
				// ItemFields: []string{
				// 	"标题",
				// 	"价格",
				// 	"链接",
				// },
				ParseFunc: func(ctx *Context) {
					dom := ctx.GetDom()
					item_list := dom.Find(".house-detail .clearfix")

					item_list.Each(func(i int, s *goquery.Selection) {
						//house_title := s.Find(".house-title a").Text()
						house_href, ok := s.Find(".house-title a").Attr("href")
						//price := s.Find(".show-price .sale-price").Text()
						if ok {
							wangzhi := ctx.GetTemp("wangzhi", "").(string)
							house_href = wangzhi + house_href
							// ctx.Output(map[int]interface{}{
							// 	0: house_title,
							// 	1: price,
							// 	2: house_href,
							// })
							ctx.AddQueue(&request.Request{
								Url:  house_href,
								Rule: "房屋信息",
							})
						}

					})
				},
			},

			"房屋信息": {
				ParseFunc: func(ctx *Context) {
					//fmt.Println("房屋信息")
					dom := ctx.GetDom()
					guide_map := dom.Find("#guideBtns li")

					guide_map.Each(func(i int, s *goquery.Selection) {
						data_src, ok := s.Find("img").Attr("data-src")
						if ok {
							//fmt.Println(data_src)
							ctx.AddQueue(&request.Request{
								Url:  data_src,
								Rule: "图片地址",
							})
						}
					})
				},
			},
			"图片地址": {
				ParseFunc: func(ctx *Context) {
					ctx.FileOutput()
				},
			},
		},
	},
}
